import pygame, math
from tiledtmxloader.helperspygame import *

class AnimatedSprite(pygame.sprite.Sprite, SpriteLayer.Sprite):
  image = None
  def __init__(self, filename, sprw, sprh, initial_location, fps=5):
    pygame.sprite.Sprite.__init__(self)
    if AnimatedSprite.image == None:
      AnimatedSprite.image = pygame.image.load(filename).convert_alpha()
    self.imagefile = AnimatedSprite.image 
    self._images = self.load_sliced_sprites(sprw,sprh, self.imagefile, 2)

    #track time we started, and time between updates
    #figure out when we need to switch image
    self._start = pygame.time.get_ticks()
    self._delay = 1000/fps
    self._last_update = 0
    self._frame = 0
    self._clock = pygame.time.Clock()

    #call update to get our first image.
    AnimatedSprite.update(self,pygame.time.get_ticks())
    self.rect = self.image.get_rect()
    self.rect.topleft = initial_location

    SpriteLayer.Sprite.__init__(self, self.image, self.rect)

  def update(self,t):
    '''moves through a list of frames'''
    if t - self._last_update > self._delay:
      self._frame += 1
      if self._frame >= len(self._images):
        self._frame = 0
      self.image = self._images[self._frame]
      self._last_update = t

  def load_sliced_sprites(self,sprw,sprh,filename,column):
    '''Loads and slices up a sprite sheet,returns a list'''
    images = []
    
    master_image = filename
    master_width, master_height = master_image.get_size()
    for i in range(master_height//sprh):
      images.append(master_image.subsurface((column*sprw,sprh*i,sprw,sprh)))
    return images

class Hero(AnimatedSprite):
  '''A sprite that can be moved with directional keys'''
  def __init__(self, filename, sprw, sprh, initial_location, fps=7):
    AnimatedSprite.__init__(self,filename, sprw, sprh, initial_location, fps)
    self.backimgs = self.load_sliced_sprites(sprw,sprh, self.imagefile, 0)
    self.rightimgs = self.load_sliced_sprites(sprw,sprh,self.imagefile, 1)
    self.frontimgs = self.load_sliced_sprites(sprw,sprh,self.imagefile, 2)
    self.leftimgs = self.load_sliced_sprites(sprw,sprh,self.imagefile, 3)
    self.direction = [0,0,1] #x,y,len
    self.move = [0,0] #x,y
    self.acel = 0.075

  def update(self, t):
    '''Updates the current from and location of the entity'''
    #get key presses
    self.direction[0] = pygame.key.get_pressed()[pygame.K_RIGHT] - \
                                        pygame.key.get_pressed()[pygame.K_LEFT]
    self.direction[1] = pygame.key.get_pressed()[pygame.K_DOWN] - \
                                        pygame.key.get_pressed()[pygame.K_UP]

    #move diagonals
    self.direction[2] = math.hypot(self.direction[0], self.direction[1])
    self.direction[2] = self.direction[2] if self.direction[2] else 1.0

    #move the sprite
    tick = self._clock.tick()
    self.move[0] = self.acel * tick * self.direction[0] / self.direction[2]
    self.move[1] = self.acel * tick * self.direction[1] / self.direction[2]

    #set sprite animation
    if self.direction[0] > 0:
      self._images = self.rightimgs
    elif self.direction[0] < 0:
      self._images = self.leftimgs
    if self.direction[1] > 0:
      self._images = self.frontimgs
    elif self.direction[1] < 0:
      self._images = self.backimgs

    #move through the frames
    if t - self._last_update > self._delay and self.move != [0,0]:
      self._frame += 1
      if self._frame >= len(self._images):
        self._frame = 0
      self.image = self._images[self._frame]
      self._last_update = t

    #no movement
    elif self.move == [0,0]:
      #determine direction and stopped sprite
      if self._images == self.leftimgs or self._images == self.rightimgs:
        self.image = self._images[2]
      else:
        self.image = self._images[1]

  def event(self, event):
    '''redundant'''
    pass

  def collision(self):
    pass


